package com.krlin.soccer.soccer.controller;

import com.krlin.soccer.soccer.entity.Complete;
import com.krlin.soccer.soccer.entity.CompletePlus;
import com.krlin.soccer.soccer.service.CompleteService;
import com.krlin.soccer.soccer.service.JudgeService;
import com.krlin.soccer.soccer.service.QueueService;
import com.krlin.soccer.soccer.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class CompleteController {

    @Autowired
    CompleteService completeService;

    @Autowired
    JudgeService judgeService;

    @Autowired
    QueueService queueService;

    /**
     * 获取所有球队信息
     * @return
     */
    @GetMapping("/completes")
    public Result getAllComplete() {
        List<Complete> allComplete = completeService.getAllComplete();
        List<CompletePlus> arrayList = new ArrayList<>();
        allComplete.forEach( e -> {
            /**
             * 处理身份证信息
             */
            String judgeName = judgeService.getJudgeById(e.getJudgeIdcard()).getJudgeName();
            e.setJudgeIdcard(judgeName+"【"+e.getJudgeIdcard()+"】");
            /**
             * 处理队伍名称
             */
            CompletePlus completePlus = new CompletePlus();
            BeanUtils.copyProperties(e, completePlus);
            log.info(e.getZhuId()+ "  " + e.getKeId());
            completePlus.setZhuName(queueService.getQueueById(e.getZhuId()).getQueueName());
            completePlus.setKeName(queueService.getQueueById(e.getKeId()).getQueueName());
            arrayList.add(completePlus);
        });
        return Result.succ(arrayList);
    }

    /**
     * 查询某一个信息
     * @param id
     * @return
     */
    @GetMapping("/complete/{id}")
    public Result getCompById(@PathVariable("id") Integer id) {
        Complete complete = completeService.selectOneById(id);
        CompletePlus completePlus = new CompletePlus();
        BeanUtils.copyProperties(complete, completePlus);
        completePlus.setZhuName(queueService.getQueueById(complete.getZhuId()).getQueueName());
        completePlus.setKeName(queueService.getQueueById(complete.getKeId()).getQueueName());
        return Result.succ(completePlus);
    }

    /**
     * 插入一条信息
     * @param complete
     * @return
     */
    @PostMapping("/complete/insert")
    public Result insertOne(@RequestBody Complete complete) {
        log.info(complete.toString());
        int res = completeService.insertOne(complete);
        if (res == 1) {
            return Result.succ("添加成功");
        } else {
            return Result.fail("添加失败");
        }
    }

    /**
     *  更新一条数据
     * @param complete
     * @return
     */
    @PutMapping("/complete/update")
    public Result updateOne(/*@PathVariable("id") Integer id,*/ @RequestBody Complete complete) {
        log.info("修改比赛信息"+complete);
        log.info(complete.toString());
        int res = completeService.updateOne(complete);
        if (res == 1) {
            return Result.succ("更新成功");
        } else {
            return Result.fail("更新失败");
        }
    }

    /**
     * 删除一条信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/complete/{id}", method = RequestMethod.DELETE)
    public Result deleteOne(@PathVariable("id") Integer id) {
        int res = completeService.deleteOne(id);
        if (res == 1) {
            return Result.succ("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }
}
